﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccessLayer;
using System.Data;

namespace BusinessLayer
{
    public class ShoppingCart
    {
        public string userid { get; set; }
        public string equip_id { get; set; }
        public string equip_name { get; set; }
        public decimal price { get; set; }
        public string image { get; set; }

        public ShoppingCart()
        {
            userid = "";
            equip_id = "";
            equip_name = "";
            price = 0;
            image = "";
        }

        public ShoppingCart(string uid,string eid, string ename, decimal prc, string img)
        {
            userid = uid;
            equip_id = eid;
            equip_name = ename;
            price = prc;
            image = img;
        }

        //GET USER ITEMS
        public List<ShoppingCart> get_cart_items_for_a_user()
        {
            List<csparameterListType> par = new List<csparameterListType>();
            List<ShoppingCart> objlist = new List<ShoppingCart>();
            par.Add(new csparameterListType("@userid", SqlDbType.NVarChar, userid));
            DataAccessLayer.csDAL objdal = new DataAccessLayer.csDAL();
            IDataReader datareader = null;
            datareader = objdal.executespreturndr("show_cart", par);
            while (datareader.Read())
            {
                ShoppingCart objP = new ShoppingCart();
                populate_reader(datareader, objP);
                objlist.Add(objP);
            }
            return objlist;
        }

        private void populate_reader(IDataReader datareader, ShoppingCart objP)
        {
            objP.equip_id = datareader.GetString(0);
            objP.equip_name = datareader.GetString(1);
            objP.price = datareader.GetDecimal(2);
            objP.image = datareader.GetString(3);
        }

        //DELETE CART ITEM
        public void delete_item()
        {
            DataAccessLayer.csDAL objdata = new DataAccessLayer.csDAL();
            List<csparameterListType> par = new List<csparameterListType>();

            par.Add(new csparameterListType("@userid", SqlDbType.NVarChar, userid));
            par.Add(new csparameterListType("@equipment_id", SqlDbType.VarChar, equip_id));

            objdata.executespreturnnd("delete_cart_item", par);
        }
    }
}